﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;


namespace PVP
{
    class CPIII
    {
        private String[] CPfile;
        public string formatText = string.Empty;
        public string filetext = string.Empty;
        public SortedList<long, Point3D> CP3 = new SortedList<long, Point3D>();

        public CPIII(String[] f)
        {
            CPfile = f;
            processFile();
        }

        private void processFile()
        {
            string str;
            Point3D pt;
            long l;
            double x;
            double y;
            double z;
            for (int i = 0; i < CPfile.Length; i++)
            {
                str = File.OpenText(CPfile[i]).ReadToEnd().TrimEnd(new char[] { '\r', '\n', ',' });
                string[] lines = str.Split('\n');
                string[] cols;
                for (int j = 0; j < lines.Length; j++)
                {
                    cols = lines[j].Split(',');
                    if (!long.TryParse(cols[0], out l))
                    {
                        throw new Exception("CPIII点编号错误！");
                    }
                    if (!double.TryParse(cols[1], out x))
                    {
                        throw new Exception("CPIII点X坐标错误！");
                    }
                    if (!double.TryParse(cols[2], out y))
                    {
                        throw new Exception("CPIII点Y坐标错误！");
                    }
                    if (!double.TryParse(cols[3], out z))
                    {
                        z = double.NaN;
                        //MessageBox.Show(z.ToString() + l);
                        //throw new Exception("CPIII点Z坐标错误！");
                    }
                    {
                        x = Math.Round(x, 4);
                        y = Math.Round(y, 4);
                        z = Math.Round(z, 4);
                    }
                    pt = new Point3D(x, y, z);
                    if (CP3.ContainsKey(l))
                    {
                        if (CP3[l].X == x && CP3[l].Y == y && CP3[l].H == z)
                        {
                            MessageBox.Show("控制点重复:" + l + " " + CPfile[i] + "\n" + CP3[l].X + "," + CP3[l].Y + "," + CP3[l].H);
                            continue;
                        }
                        else
                        {
                            MessageBox.Show("控制点" + l + "重复:" + " " + CPfile[i] + "\n" + CP3[l].X + "," + CP3[l].Y + "," + CP3[l].H
                                            + "\n" + l.ToString() + "," + x.ToString() + "," + y.ToString() + "," + z.ToString());
                            continue;
                        }
                    }
                    else
                    {
                        CP3.Add(l, pt);
                    }
                }
            }
            foreach (KeyValuePair<long, Point3D> kvp in CP3)
            {
                formatText += kvp.Key + ": " + kvp.Value.X + ", " + kvp.Value.Y + ", " + kvp.Value.H + "\n";
                filetext += kvp.Key + "," + kvp.Value.X + "," + kvp.Value.Y + "," + kvp.Value.H + "\n";
            }
        }
    }
}
